package org.example.MVC2;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MyInterceptor implements HandlerInterceptor {      //实现HandlerInterceptor接口的拦截器MyInterceptor

    @Override         //该方法用于对程序进行安全控制、权限效验等，它会在控制器方法调用前执行
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor.........preHandle");
        //对拦截的请求进行处理
        return true;
    }

    @Override          //该方法用于对请求域中的模型和视图做进一步的修改，它会在控制器方法调用之后且视图解析之前执行
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("MyInterceptor.........postHandle");
    }

    @Override          //该方法可以完成一些资源清理、日志信息记录等工作，它会在整个请求完成后执行，即视图渲染结束之后执行
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("MyInterceptor.........afterCompletion");
    }
}
